package demo;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	private Socket socket;
	private String clientName;

	public Client() {
		try {
			socket = new Socket(Server.IP, Server.PORT);
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
			clientName = bufferedReader.readLine();

			DataOutputStream dataOutputStream=new DataOutputStream(socket.getOutputStream());
			dataOutputStream.writeUTF(clientName);

			new Thread(new ReceiveMessage()).start();

			new Thread(new SendMessage()).start();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private class  ReceiveMessage implements  Runnable{


		public void run() {
			try {
				DataInputStream dataInputStream=new DataInputStream(socket.getInputStream());
				String message="";
				while (true){
				message=dataInputStream.readUTF();

				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	private class SendMessage implements Runnable {

		public void run() {
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			String message = "";
			try {

				DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
				while (true){
					message = bufferedReader.readLine();
				dataOutputStream.writeUTF(message);}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}



	public static void main(String[] args) {
		Client client = new Client();
	}








}
